<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="<?php echo __ACSS__;?>/font.css">
        <link rel="stylesheet" href="<?php echo __ACSS__;?>/xadmin.css">
        <!-- <link rel="stylesheet" href="./css/theme5.css"> -->
        <script src="<?php echo __ALIB__;?>/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="<?php echo __AJS__;?>/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
        <script src="<?php echo __AJS__;?>/html5.min.js"></script>
        <script src="<?php echo __AJS__;?>/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="cat_name" class="layui-form-label">
                        <span class="x-red">*</span>所属分类
                    </label>
                    <div class="layui-input-inline">
                        <select name="c_id" id="c_id">
                            <?php foreach($admin_cate as $v){ ?>
                                <option value="<?php echo $v['id']; ?>"><?php echo $v['title']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="sort" class="layui-form-label">
                        <span class="x-red">*</span>权限名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" placeholder="如：新增分类" autocomplete="off" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="cat_name" class="layui-form-label">
                        <span class="x-red">*</span>权限url</label>
                    <div class="layui-input-inline">
                        <input type="text" id="url" name="url" placeholder="如：admin/adminRule/ruleAdd" autocomplete="off" class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>请输入控制器(大小写敏感)</div></div>
                <label for="L_repass" class="layui-form-label"></label>
                <label class="layui-btn" onclick="add()">增加</label>
            </form>
        </div>

    </div>
    </body>
    <script>
      layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var form = layui.form;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

      function add() {
          var c_id = $("#c_id option:selected").val();
          var name = $("#name").val();
          var url = $("#url").val();

          if (name.length < 1) {
              layer.alert('权限名称不能为空', {
                  icon: 5
              });
              return false;
          }
          if (url.length < 1) {
              layer.alert('权限url不能为空', {
                  icon: 5
              });
              return false;
          }else{
              if (!new RegExp(/^([A-Za-z]{4,5}[\/]{1}[A-Za-z]{3,20}[\/]{1}[A-Za-z]{3,20})$/).test(url)) {
                  layer.alert('权限url不合法', {
                      icon: 5
                  })
                  return false;
              }
          }
          //异步提交
          $.post("/index.php/admin/adminRule/ruleAdd",{c_id:c_id,name:name,url:url}, function (res) {
              res = JSON.parse(res);
              if (res.code==500){
                  layer.alert(res.data, {
                      icon: 5
                  })
              }else{
                  layer.alert(res.data, {
                      icon: 6
                  }, function() {
                      //关闭当前frame
                      xadmin.close();
                      // 可以对父窗口进行刷新
                      xadmin.father_reload();
                  });
                  return false;
              }
          });
//                    $.ajax({
//                        url: "<?php echo ADMIN;?>/catadd.php",
//                        type: "post",
//                        dataType:'JSON',
//                        data:data.field,
//                        success: function (res) {
//                            if (res.code==500){
//                                layer.alert(res.data, {
//                                    icon: 5
//                                })
//                            }else{
//                                layer.alert(res.data, {
//                                    icon: 6
//                                }, function() {
//                                    //关闭当前frame
//                                    xadmin.close();
//                                    // 可以对父窗口进行刷新
//                                    xadmin.father_reload();
//                                });
//                            }
//                        },
//                        error:function (res) {
//                            console.log('false='+res);
//                        }
//                    });
      };

    </script>
</html>